package com.zys.el.user.controller;

import com.zys.el.core.controller.BaseController;
import com.zys.el.core.po.ResponseBean;
import com.zys.el.user.po.BookPdf;
import com.zys.el.user.po.Field;
import com.zys.el.user.service.IBookpdfService;
import com.zys.el.user.service.IFieldMenuService;
import com.zys.el.user.service.IFieldService;
import com.zys.el.user.util.FieldUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/book-pdf")
public class BookpdfController extends BaseController<IBookpdfService, BookPdf> {

    @Autowired
    private IFieldService iFieldService;

    @Autowired
    private IFieldMenuService iFieldMenuService;

    @Autowired
    private FieldUtil fieldUtil;

    @ApiOperation(value = "保存", notes = "ID存在修改，不存在添加")
    @PostMapping("/new-save")
    public ResponseBean save(@RequestBody BookPdf entity) throws Exception {
//        System.out.println(entity);//前端传来的entity是带有领域列表的
        ResponseBean rm = new ResponseBean();
        Field field = new Field();
        try {
            service.saveOrUpdate(entity);
            if (entity.getFields() != null) {
                List<String> fields = entity.getFields();
                fieldUtil.FieldHandle(fields, entity.getId());
            }
            rm.setModel(entity);
        } catch (Exception e) {
            e.printStackTrace();
            rm.setSuccess(false);
            rm.setMsg("保存失败");
        }
        return rm;
    }


}
